import std.stdio: writefln;
import std.string;

import derelict.sdl.sdl;

import Utils;
import Pixels;
import Point;

struct fillLine{
	point pkt[2];

	static fillLine opCall(){
		fillLine res;
		return res;
	}

	char[] toString(){
		return format("[(%d, %d), (%d, %d)]", pkt[0].x, pkt[0].y, pkt[1].x, pkt[1].y);
	}

}

public class ScanLine{
	uint polygonColor;
	fillLine[] lines;

	this(SDL_Surface *surface, uint polygonColor = foreground){
		this.polygonColor = polygonColor;
		scan(surface);
	}

	void scan(SDL_Surface *surface){
		lines.length = 0;
		uint *p = cast(uint*)surface.pixels;
		bool first = true;
		fillLine ll;
	writefln(ll);
		for(int i = 0; i < yResolution; i++){
			int currLine = xResolution*i;
			first = true;
			for(int j = 0; j < xResolution; j++){
				if(p[currLine + j] == polygonColor){
					if(first){
						ll.pkt[0] = point(j,i);
						first = false;
					}
					else{
						ll.pkt[1] = point(j,i);
						lines ~= ll;
						lines.length = lines.length + 1;
						first = true;
					}
				}	
			}
		}
		writefln(lines);
	}

	void draw(SDL_Surface *surface){
		uint *p = cast(uint*)surface.pixels;
		foreach(line;lines)
			p[(line.pkt[0].x + xResolution*line.pkt[0].y)..(line.pkt[1].x + xResolution*line.pkt[0].y)] = foreground;
	}
}
